package com.example.neusoft.yuejian_12.Fragment;

import android.content.Intent;
import android.media.Image;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.neusoft.yuejian_12.Activity.AddPhotoActivity;
import com.example.neusoft.yuejian_12.Adapter.PhotoAdapter;
import com.example.neusoft.yuejian_12.JavaBean.Photo;
import com.example.neusoft.yuejian_12.Listener.PhotoListener;
import com.example.neusoft.yuejian_12.Model.PhotoModel;
import com.example.neusoft.yuejian_12.R;

import java.util.List;

public class PhotoFragment extends Fragment  implements PhotoListener {
    private RecyclerView recyclerView;
    private ImageButton photo_add;
    List<Photo> list;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_photo, container, false);
        initRecyclerView(view);
        photolist();
        return  view;
    }

    public  void initRecyclerView(View view){
        recyclerView=(RecyclerView) view.findViewById(R.id.rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        photo_add = (ImageButton) view.findViewById(R.id.photo_add);
        photo_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(getActivity(),AddPhotoActivity.class);
                startActivity(i);
                refresh();
            }
        });
    }

    private void photolist() {
        PhotoModel model=new PhotoModel();
        model.getPhotoList("id","title",this);
    }

    @Override
    public void onResponse(List<Photo> listBeen) {
        list=listBeen;
        recyclerView.setAdapter(new PhotoAdapter(getActivity(),list));
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(getActivity(), "fail"+msg, Toast.LENGTH_SHORT).show();
    }
    public void refresh(){
        photolist();
    }

}